package cat.joronya.supermanager;

import java.util.Random;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class SupervermutWidgetProvider extends AppWidgetProvider 
{
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		// agafem totes les instancies del nostre widget
		ComponentName thisWidget = new ComponentName(context,
				SupervermutWidgetProvider.class);
		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		
		// updatem cada instancia
		for (int widgetId : allWidgetIds) 
		{
			// Create some random data
			int number = (new Random().nextInt(100));
			Log.w("SupervermutWidgetProvider.onUpdate: WidgetExample", String.valueOf(number));

			// actualitzem el widget
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
			//remoteViews.setTextViewText(R.id.tancament_value, String.valueOf(number));

			// registrem el onClickListener
			Intent intent = new Intent(context, SupervermutWidgetProvider.class);

			intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
			intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

			PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
					0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			remoteViews.setOnClickPendingIntent(R.id.widget_row4, pendingIntent);
			appWidgetManager.updateAppWidget(widgetId, remoteViews);
		}
	}
}
